﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  SetAdd.aspx.cs
 作    者:范徐超
 编写日期:2009-09-15
 审    核:
 审核日期:
 描    述:套餐新增页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

public partial class Business_Booking_SetAdd : WebUI.Page.BasePage
{
    private DishBO dishBO = new DishBO();
    private DishMO dishMO = new DishMO();
    private SetBO setBO = new SetBO();
    private SetMO setMO = new SetMO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindSetDish();
        }
    }

    #region 按钮事件
    /// <summary>
    /// 新增事件
    /// </summary>
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        int i = 0;
        string strError = "";
        setMO = GetModel();
        //判断是否有重名套餐
        if (IsReapte(tb_SetName.Text.Trim()))
        {
            PageError("该套餐名称已经存在！", Request.RawUrl);
        }
        if(setBO.IsExsistSet(tb_SetCode.Text.Trim()))
        {
            PageError("该套餐代码已经存在！", Request.RawUrl);
        }
        try
        {
            i = setBO.Add(setMO);
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        if (i > 0)
        {
            PageRight("操作成功！", "SetManagement.aspx", "返回管理", "");
        }
        else
        {
            PageError(strError, Request.RawUrl);
        }
    }
    /// <summary>
    /// 返回事件
    /// </summary>
    protected void butrReset_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("SetManagement.aspx");
    }
    #endregion

    #region 填充规则实体类 private RuleMO GetModel()
    /// <summary>
    /// 填充规则实体类 
    /// </summary>
    private SetMO GetModel()
    {
        string setname = tb_SetName.Text.Trim();
        string setcode = tb_SetCode.Text.Trim();
        string setprice = tb_SetPrice.Text.Trim();
        string setpic = GetImagePath();
        string setremark = tb_SetRemark.Text.Trim() == "" ? "无" : tb_SetRemark.Text;

        setMO.SetID = setBO.GetMaxId();
        setMO.SetName = setname;
        setMO.SetCode = setcode;
        setMO.SetPic = setpic;
        setMO.SetPrice = Convert.ToDecimal(setprice);
        setMO.SetDish = GetDishIDs();
        setMO.SetRemark = setremark;

        return setMO;
    }
    #endregion

    #region 获得上传控图片路径
    /// <summary>
    /// 获得上传控图片路径
    /// </summary>
    private string GetImagePath()
    {
        string path = "";
        //if (ViewState["ImagePath"] != null)
        //{
        //    path = ViewState["ImagePath"].ToString();
        //}

        return path;
    }
    #endregion

    #region 绑定菜式
    /// <summary>
    /// 绑定菜式
    /// </summary>
    private void BindSetDish()
    {
        DataSet ds = dishBO.GetAllList();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ListItem li = new ListItem();
                li.Text = dr["DishName"].ToString() + "     " + 
                    dr["DishPrice"].ToString().Split('.')[0] + "." + dr["DishPrice"].ToString().Split('.')[1].Substring(0,2) + "元";
                li.Value = dr["DishID"].ToString();

                lb_SetDish.Items.Add(li);
            }
        }
    }
    #endregion

    #region 图片上传事件
    protected void lbtn_UploadImage_Click(object sender, EventArgs e)
    {
        //if (Page.IsValid)
        //{
        //if (!string.IsNullOrEmpty(InputFile1.FileName))
        //{
        //    if (InputFile1.HasFile)
        //    {
        //        //文件名
        //        string filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(InputFile1.FileName);
        //        //保存文件路径
        //        string filefullname = System.IO.Path.Combine(Request.PhysicalApplicationPath + "\\Upload", filename);
        //        //上传文件
        //        InputFile1.MoveTo(filefullname, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
        //        //显示讯息
        //        double size = Math.Round(InputFile1.ContentLength / 1024F, 2);
        //        //Label1.Text = string.Format("来源:{0} ({1}kb)<br/>上传文件名称：{2}。", InputFile1.FileName, size, filename);

        //        //image1.Src = Server.MapPath(filefullname.Replace(@"\\",@"\"));
        //        Image1.ImageUrl = "../../../Upload/" + filename;

        //        ViewState["ImagePath"] = Image1.ImageUrl;
        //    }
        //}
        ////}
    }
    #endregion

    #region 选择菜式事件
    protected void lb_SetDish_SelectedIndexChanged(object sender, EventArgs e)
    {
        double AllPrice = 0.0;
        foreach (ListItem li in lb_SetDish.Items)
        {
            if (li.Selected == true)
            {
                dishMO = dishBO.GetModel(int.Parse(li.Value));
                AllPrice += Convert.ToDouble(dishMO.DishPrice);
            }
        }

        lbl_AllCount.Text ="合计：" + AllPrice.ToString() + "元";
    }
    #endregion

    #region 获取菜式ID string GetDishIDs()
    private string GetDishIDs()
    {
        string strIDs = "";
        foreach (ListItem li in lb_SetDish.Items)
        {
            if (li.Selected == true)
            {
                strIDs += li.Value + ",";
            }
        }
        return strIDs;
    }
    #endregion

    #region 判断是否有重名套餐 bool IsReapte(string BoxName)
    /// <summary>
    /// 判断是否有重名套餐,有重名返回true,无重名返回false
    /// </summary>
    /// <param name="BoxName">新增的包厢名称</param>
    private bool IsReapte(string SetName)
    {
        bool flag = false;
        DataSet ds = setBO.GetAllList();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (SetName == dr["SetName"].ToString())
                {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
    #endregion
}
